<!---
LICENSE INFORMATION:

Copyright 2007, Hem Talreja

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License.

You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

VERSION INFORMATION:	(0.1)

 --->
<cfcomponent name="dbReader" extends="dbHelper">

	<cffunction name="init" returntype="dbReader" access="public" output="false">
		<cfargument name="dbProperties" required="true" type="any" />
		<cfset setDBProperties(Arguments.dbProperties) />
		<cfreturn this />
	</cffunction>



	<cffunction name="LookUP" access="package" returntype="string" output="false" hint="Database Loook up queries">
		<cfargument name="selectClause" required="true" type="string" hint="The select clause for the query" />
		<cfargument name="fromTable" required="true" type="string" hint="The table to select from" />
		<cfargument name="whereClause" required="false" default="" type="string" hint="The table to select from" />
		<cfargument name="defaultValue" required="false" default="" type="string" hint="The value to return if no record was fount" />

		<cfscript>
			var strReturn = "";
			var strQuery = "SELECT #Arguments.SelectClause# AS LookUpVal FROM #Arguments.FromTable# ";

			if( Arguments.whereClause IS NOT "" ){
				strQuery = strQuery & " WHERE " & Arguments.whereClause;
			}
		</cfscript>

		<cftry>
			<cfquery name="qryLookUp" datasource="#getDBProperties().getDataSource()#">
				#PreserveSingleQuotes(strQuery)#
			</cfquery>

			<cfcatch type="any">
				<cfrethrow />
			</cfcatch>
		</cftry>

		<cfif qrylookup.recordcount>
			<cfset strReturn = qryLookUp.LookUpVal />
		<cfelse>
			<cfset strReturn = Arguments.defaultValue />
		</cfif>

		<cfreturn strReturn />
	</cffunction>


	<cffunction name="Select" access="package" returntype="query" output="false" hint="will return a query object">
		<cfargument name="SelectClause" required="true" type="string" hint="The select clause for the query" />
		<cfargument name="FromClause" required="true" type="string" hint="The table to select from" />
		<cfargument name="WhereClause" required="false" default="" type="string" hint="The table to select from" />
		<cfargument name="OrderByClause" required="false" default="" type="string" hint="OrderBy clause" />
		<cfargument name="MaxRows" required="true" default="0" type="numeric" />
		<cfargument name="CachedWithin" required="false" type="string" default="" />
		<cfargument name="QueryName" required="false" type="string" default="selRecords" />


		<!--- local variables for query  --->
		<cfscript>
			var selRecords = "";
			var strQrySting = "";
			var intMaxRows = Arguments.MaxRows;

			var strTopSQL = "";
			var strWhereSQL = "";
			var strLimitSQL = "";

			var blnCachedQry = false;
			var intDays = 0;
			var intMths = 0;
			var intHrs = 0;
			var intMins = 0;

			var dbSelectQry = StructNew();
		</cfscript>

		<cfif intmaxrows>
			<cfswitch expression="#getDBProperties().getDBType()#">
				<cfcase value="MsSQL,MSAccess">
					<cfset strTopSQL = " TOP #intMaxRows# " />
				</cfcase>
				<cfcase value="MySQL">	<!--- MY SQL --->
					<cfset strLimitSQL = " LIMIT #intMaxRows# " />
				</cfcase>
				<cfcase value="ORACLE">	<!--- Oracle --->
					<cfif arguments.whereClause is not "">
						<cfset strWhereSQL = " ROWNUM <= #intMaxRows# AND " />
					<cfelse>
						<cfset strWhereSQL = " ROWNUM <= #intMaxRows# " />
					</cfif>
				</cfcase>
			</cfswitch>
		</cfif>

		<cfset strQryString = " SELECT #strTOpSQL# #Arguments.SelectClause# FROM #Arguments.FROMClause# " />

		<cfif arguments.whereClause is not "">
			<cfset strQryString = strQryString & " WHERE #strWhereSQL# #Arguments.WhereClause# " />
		</cfif>

		<cfif arguments.orderByClause is not "">
			<cfset strQryString = strQryString & " ORDER BY #Arguments.OrderBYClause# " />
		</cfif>

		<cfif strLimitSQL is not "">
			<cfset strQryString = strQryString & strLimitSQL />
 		</cfif>


		<cfif listlen(arguments.cachedWithin) eq 4 >

			<cfset blncachedqry = true />
			<cfset intDays = val( listgetat( arguments.cachedwithin, 1) ) />
			<cfset intMths = val( listgetat( arguments.cachedwithin, 2) )  />
			<cfset intHrs  = val( listgetat( arguments.cachedwithin, 3) )  />
			<cfset intMins = val( listgetat( arguments.cachedwithin, 4) )  />

		</cfif>

		<cftry>

			<cfif not blncachedqry >

				<cfquery name="dbSelectQry.#QueryName#" datasource="#getDBProperties().getDataSource()#">
					#preserveSingleQuotes(strQryString)#
				</cfquery>

			<cfelse>

				<cfquery name="dbSelectQry.#QueryName#" datasource="#getDBProperties().getDataSource()#" cachedwithin="#createTimeSpan(intDays,intMths,intHrs,intMins)#" >
					#preserveSingleQuotes(strQryString)#
				</cfquery>

			</cfif>

			<cfcatch type="any">
				<cfrethrow />
			</cfcatch>
		</cftry>


		<cfreturn dbselectqry[QueryName] />	<!--- getRecords --->
	</cffunction>

</cfcomponent>